<?php
  // no direct access
  defined( '_JEXEC' ) or die( 'Restricted access' );
  jimport( 'joomla.application.component.view');
  
  class TeamGameStatViewTeamGameStat extends JView
  {              
    var $_name = "teamgamestat";
  
    function display()
    { 
      $this->displayList();
    }
    
    function displayList()
    {
      /* sorting */
      $state =& $this->get( 'state' );
            $lists['order_Dir'] = $state->get($this->_name . 'filter_order_Dir' );
            $lists['order']     = $state->get($this->_name . 'filter_order' );
            $this->assignRef('lists', $lists);    

      $teamgamestat   = $this->getModel('teamgamestat');
      $stats = $teamgamestat->get_list_summary();
      $this->assign('stats', $stats);
      
      $pagination = $this->get('Pagination');
      $this->assign('pagination', $pagination);
      
      JToolBarHelper::title( JText::_( 'Team Game Stats' ), 'generic.png' );  
      
      JToolBarHelper::publishList();
      JToolBarHelper::unpublishList();
      JToolBarHelper::addNew();
      JToolBarHelper::editList();
      JToolBarHelper::deleteList();
      
      PBNYHelper::createMenu();
      
      parent::display("list");    
    }
    
    function displayAdd()
    {    
      /* sorting */
      $state =& $this->get( 'state' );
            $lists['order_Dir'] = $state->get($this->_name . 'filter_order_Dir' );
            $lists['order']     = $state->get($this->_name . 'filter_order' );
            $this->assignRef('lists', $lists);    
    
      $team        = $this->getModel('team');
      $teams          = $team->get();
      $this->assignRef('teams', $teams);
      
      JToolBarHelper::title( JText::_( 'Add Team Game Stat' ), 'generic.png' );
      JToolBarHelper::save();
      JToolBarHelper::cancel();  

      parent::display("edit");
    }    
    
    function displayEdit()
    {    
      
      $cids = JRequest::getVar('cid', null, 'default', 'array' );
      
      $mteam      = $this->getModel('team');
      $mschedule     = $this->getModel('schedule');
      $teamgamestat   = $this->getModel('teamgamestat');
      $venue          = $this->getModel('venue');
      
      $stat       = $teamgamestat->get($cids[0]);
      $this->assign('stat', $stat);
      
      $team           = $mteam->get($stat->team_id);
      $this->assign('teams', array($team));
      
      $schedule           = $mschedule->get($stat->schedule_id);
      $old_data           = $schedule->schedule;
      
      if($team->id == $schedule->first_team_id){
        $team1 = $team->name;
        $team2 = $mteam->get($schedule->second_team_id)->name;
      }
      else if($team->id == $schedule->second_team_id){
        $team2 = $team->name;
        $team1 = $mteam->get($schedule->first_team_id)->name;
      }
      
      $court              = $venue->get($schedule->venue_id)->name;
      $schedule->schedule = "$old_data   $team1 vs. $team2 @ $court";
      $this->assign('schedules', array($schedule));

      JToolBarHelper::title( JText::_( 'Edit Team Game Stat' ), 'generic.png' );
      JToolBarHelper::save();
      JToolBarHelper::cancel();  

      parent::display("edit");
    }    
  }
?>